package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.EmptyBorder;

import org.apache.commons.lang3.StringUtils;

import bean.ApplicationConfiguration;
import bean.Message;
import bean.Server;
import bean.UserProfile;
import business.Connection;
import business.ConnectionThread;
import business.MessageHandler;
import business.MessageReceiver;

public class MainWindow extends JFrame {
	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	private JTextField textField;
	private JTextArea textArea;
	private MessageReceiver receiver;
	private ApplicationConfiguration applicationConfiguration;
	private JScrollPane scrollPane;
	private Connection connection;

	protected boolean debugMode = false;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow frame = new MainWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent e) {
				prepareApplication();
				initApplication();
			}
		});

		setTitle("Dimension IRC Client 2");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					String text = textField.getText();
					connection.sendText(MessageHandler
							.processSendTextCommand(text));
					textField.setText(null);
				}
			}
		});
		contentPane.add(textField, BorderLayout.SOUTH);
		textField.setColumns(10);

		textArea = new JTextArea();
		textArea.setEditable(false);

		scrollPane = new JScrollPane();
		scrollPane.setViewportView(textArea);
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		contentPane.add(scrollPane, BorderLayout.CENTER);

		receiver = new MessageReceiver() {

			@Override
			public void setMessage(Message message) {
				StringBuilder builder = new StringBuilder(textArea.getText());
				textArea.setText(null);

				builder.append(message.getFormattedDateTime());
				builder.append(StringUtils.EMPTY);
				if (debugMode) {
					builder.append(message.getReceiver());
					builder.append(" - ");
					builder.append(message.getCommand());
					builder.append(" - ");
					builder.append(message.getCode());
				}
				builder.append(" <");
				builder.append(message.getSender());
				builder.append("> ");
				builder.append(message.getText());
				builder.append("\n");

				textArea.setText(builder.toString());
				textArea.setCaretPosition(textArea.getText().length());
			}
		};

	}

	private void initApplication() {
		connection = new Connection(applicationConfiguration);
		connection.connect();

		Thread connectionThread = new ConnectionThread(connection, receiver);
		connectionThread.start();
	}

	private void prepareApplication() {
		UserProfile userProfile = new UserProfile();
		userProfile.setNickName("dimeTest");

		String host = "irc.freenode.net";
		int port = 6667;

		Server server = new Server(host, port, userProfile);

		applicationConfiguration = new ApplicationConfiguration(server);
	}

}
